package com.saifan.wyy_ov.ui.property;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.data.bean.RepairBean;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.utils.CallPhoneUtil;
import com.saifan.wyy_ov.utils.StringUtil;


/**
 * @version V1.0.0
 * @author: lomasky
 * @date: 2015-5-21 10:18:38
 * @descrption
 */

public class ProgressItemActivity extends BaseActivity {
    private Toolbar toolbar;

    RepairBean item;

    private ImageView line1;
    private TextView date1;
    private LinearLayout layout1;

    private ImageView line2;

    private LinearLayout layout2;

    private ImageView line3;
    private TextView date3;
    private LinearLayout layout3;
    private TextView date4;
    private LinearLayout layout4;
    private TextView JDR;
    private TextView LXDH;
    private TextView BT;
    private TextView lr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        item = (RepairBean) getIntent().getSerializableExtra("data");

        setContentView();
    }

    public void setContentView() {
        setContentView(R.layout.activity_progress_item);
        this.BT = (TextView) findViewById(R.id.BT);
        this.lr = (TextView) findViewById(R.id.lr);
        this.LXDH = (TextView) findViewById(R.id.LXDH);
        this.JDR = (TextView) findViewById(R.id.JDR);
        this.layout4 = (LinearLayout) findViewById(R.id.layout4);
        this.date4 = (TextView) findViewById(R.id.date4);
        this.layout3 = (LinearLayout) findViewById(R.id.layout3);
        this.date3 = (TextView) findViewById(R.id.date3);
        this.line3 = (ImageView) findViewById(R.id.line3);
        this.layout2 = (LinearLayout) findViewById(R.id.layout2);
        this.line2 = (ImageView) findViewById(R.id.line2);
        this.layout1 = (LinearLayout) findViewById(R.id.layout1);
        this.date1 = (TextView) findViewById(R.id.date1);
        this.line1 = (ImageView) findViewById(R.id.line1);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        BT.setVisibility(TextUtils.isEmpty(item.getWYBSQD_BSLX())?View.GONE:View.VISIBLE);
        BT.setText(StringUtil.returnShow(item.getWYBSQD_BSLX()));
        lr.setText(StringUtil.returnShow(item.getWYBSQD_BSLR()));

        layout1.setVisibility(View.VISIBLE);
        line1.setVisibility(View.INVISIBLE);
        line2.setVisibility(View.INVISIBLE);
        line3.setVisibility(View.INVISIBLE);
        date1.setText(StringUtil.returnShow(item.getWYBSQD_FSSJ()));

        if (item.isWYBSQD_SFJD()) {
            layout2.setVisibility(View.VISIBLE);
            line1.setVisibility(View.VISIBLE);
            line2.setVisibility(View.INVISIBLE);
            JDR.setText(StringUtil.returnShow(item.getWYBSQD_JDR()));
            LXDH.setText(StringUtil.returnShow(item.getWYBSQD_JDRDH()));
        }
        if (item.isWYBSQD_SFWC()) {
            layout3.setVisibility(View.VISIBLE);
            line1.setVisibility(View.VISIBLE);
            line2.setVisibility(View.VISIBLE);
            line3.setVisibility(View.INVISIBLE);
            date3.setText(StringUtil.returnShow(item.getWYBSQD_WGSJ()));

        }
        if (item.isWYBSQD_SFHF()) {
            layout4.setVisibility(View.VISIBLE);
            line1.setVisibility(View.VISIBLE);
            line2.setVisibility(View.VISIBLE);
            line3.setVisibility(View.VISIBLE);
            date4.setText(StringUtil.returnShow(item.getWYBSQD_HFSJ()));

        }

    }


    public void call(View v) {
        if (!StringUtil.isNullOrEmpty(item.getWYBSQD_JDRDH())) {

            if (Build.VERSION.SDK_INT >= 23) {

                int checkCallPhonePermission = ContextCompat.checkSelfPermission( this, Manifest.permission.CALL_PHONE);
                if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(ProgressItemActivity.this, new String[]{Manifest.permission.CALL_PHONE}, CALL_PHONE);
                    return;
                } else {
                    CallPhoneUtil.callPhone(ProgressItemActivity.this,item.getWYBSQD_JDRDH());


                }
            } else {

                CallPhoneUtil.callPhone(ProgressItemActivity.this,item.getWYBSQD_JDRDH());

            }


        }

    }



    private static final int CALL_PHONE = 123;
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == CALL_PHONE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                CallPhoneUtil.callPhone(ProgressItemActivity.this,item.getWYBSQD_JDRDH());


            } else {
                // Permission Denied
                Toast.makeText(this, "您禁用拨打电话权限", Toast.LENGTH_SHORT)
                        .show();

            }
        }

    }

}
